SpringBoot 启动类示例:1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
通过 SpringApplication 静态方法 run 执行启动:
1 | public static ConfigurableApplicationContext run(Object source, String... args) { |
调用构造函数:1
2
3
4
5
6
7
8
9public SpringApplication(Object... sources) {
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = new HashSet();
this.initialize(sources);
}
核心方法 initalize :1
2
3
4
5
6
7
8
9
10private void initialize(Object[] sources) {
if (sources != null && sources.length > 0) {
this.sources.addAll(Arrays.asList(sources));
}
this.webEnvironment = this.deduceWebEnvironment();
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = this.deduceMainApplicationClass();
}
首先:把 DemoApplication 类对象设置到 SpringApplication 持有的 sources 属性中,是个 Set 集合
其次:判断是否是 Web 程序环境
第三:设置初始化器
第四:设置事件监听器
第五:找出程序入口